import vue from 'vue';
export default {
	validate(self, obj, rules) {
		this.self = self;
		this.obj = obj;
		for(let item of rules) {
			let one_rules = item.rule;
			let value = this.obj[item.name];
			if(item.emptyOrRequire && !this.obj[item.name]) {
				continue;
			} else {
				for(let item_ of one_rules) {
					if(!this[item_](item)) {
						return false;
					}
				}
			}
		}
		return true;
	},
	validateEmpty_(currentItem) {
		let value = this.obj[currentItem.name]
		if(value === '' || value === null) {
			this.self.$message(currentItem.label + '不能为空！');
			console.log(currentItem)
			return false;
		}
		return true;
	},
	validateSelectEmpty_(currentItem) {
		let value = this.obj[currentItem.name]
		if(value === '' || value === null || value === undefined) {
			this.self.$message( '请选择' +  currentItem.label);
			return false;
		}
		return true;
	},
	validateUploadEmpty_(currentItem) {
		
		let value = this.obj[currentItem.name];
		if(value === '' || value === null) {
			this.self.$message( '请上传' +  currentItem.label);
			console.log(currentItem)
			return false;
		}
		return true;
	},
	validatePwd(currentItem){ //密码
		if(!this.validateEmpty_(currentItem)) {
			this.self.$message( currentItem.label + '不能为空！');
			return false;
		}
		let value = this.obj[currentItem.name];
		if(value.length<6){
			this.self.$message( currentItem.label + '长度不能小于六位');
			return false;
		}
		if(value.length>10){
			this.self.$message( currentItem.label + '长度不能大于十位');
			return false;
		}
		return true;
	},
	validateCode(currentItem){ //验证码
		if(!this.validateEmpty_(currentItem)) {
			this.self.$message('验证码不能为空！');
			return false;
		}
		let value = this.obj[currentItem.name];
		var reg = /^[0-9]\d{5}/;
		if(!reg.test(value)){
			this.self.$message('请输入六位数验证码!');
			return false;
		}
		return true;
	},
	validateEmptyCity_(currentItem) {
		let value = this.obj[currentItem.name]
		if(value === '' || value === null) {
			this.self.$message('请先在地图上选择地址！');
			console.log(currentItem)
			return false;
		}
		return true;
	},
	validateN_(currentItem) {
		let value = this.obj[currentItem.name];
		if(!(value>0)) {
			this.self.$message(currentItem.label + '必须大于0！');
			return false;
		}
		return true;
	},
	validateNum_(currentItem) {
		if(!this.validateEmpty_(currentItem)) {
			return false;
		}
		let value = parseInt(this.obj[currentItem.name]);
		var reg=/^\d{12,22}$/;
		if(!reg.test(value)){
			this.self.$message('请填写正确的银行卡号！');
			return false;
		}
		return true;
	},
	validatePrice_(currentItem) {
		if(!this.validateEmpty_(currentItem)) {
			return false;
		}
		let value = this.obj[currentItem.name];
		var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
		if(!reg.test(value)){
			this.self.$message('请填写正确的金额格式！');
			return false;
		}
		return true;
	},
	validateMail_(currentItem) {
		if(!this.validateEmpty_(currentItem)) {
			return false;
		}
		let value = this.obj[currentItem.name];
		var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
		if(!reg.test(value)){
			this.self.$message('请填写正确的邮箱格式！');
			return false;
		}
		return true;
	},
	validateNumfloat_(currentItem){
		if(!this.validateEmpty_(currentItem)) {
			return false;
		}
		let value = this.obj[currentItem.name];
		var reg=/^[0-9]+\.{0,1}[0-9]{0,2}$/;
		if(!reg.test(value)){
			this.self.$message('请填写正确的数值！');
			return false;
		}
		return true;
	},
	validateID_(currentItem) {
		if(!this.validateEmpty_(currentItem)) {
			return false;
		}
		let value = this.obj[currentItem.name]
		let myreg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
		console.log('身份证验证')
		if(!myreg.test(value)) {
			this.self.$message('身份证格式不正确！');
			return false;
		}
		return true;
	},
	validateSocialCreditCode(currentItem){
		if(!this.validateEmpty_(currentItem)) {
			return false;
		}
		let value = this.obj[currentItem.name]
		var patrn = /^[0-9A-Z]+$/;
		  //18位校验及大写校验
		  console.log(patrn.test(value));
		  if ((value.length != 18) || !patrn.test(value)) {
		  	this.self.$message('不是有效的企业统一社会信用代码！');
		    return false;
		  }
		return true;
	},
	validateTel_(currentItem) {
		if(!this.validateEmpty_(currentItem)) {
			return false;
		}
		let value = this.obj[currentItem.name]
		let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
//		if(!myreg.test(value)) {
//			this.self.$message('手机号格式不正确！');
//			return false;
//		}
		if(!value){
			this.self.$message('手机号不能为空！');
			return false;
		}else if(!myreg.test(value)){
			this.self.$message('手机号格式不正确！');
			return false;
		}
		return true;
	},
	validateUpload_(currentItem) {
		let value = this.obj[currentItem.name]
		if(value <= 0) {
			this.self.$message('请上传' + currentItem.label + "！");
			return false;
		}
		return true;
	},
	validateSelect_(self, fieldValue, noticeMsg) {
		if(!fieldValue) {
			this.self.$message('请选择' + noticeMsg + "！");
			return false;
		}
		return true;
	},
	validateImgSizeAndType_(self, file, imgSize, imgTypes) {
		if(imgTypes == undefined) {
			imgTypes = '.png|.jpg|.jpeg|.gif'
		}
		let imgType = file.name.substr(file.name.lastIndexOf('.') + 1);
		if(imgTypes.indexOf(imgType.toLowerCase()) == -1) {
			self.$message('只能上传' + imgTypes + '格式图片!');
			return false;
		}
		if(file.size / 1024 / 1024 > imgSize) {
			self.$message('上传头像图片大小不能超过 ' + imgSize + 'MB!');
			return false;
		}
		return true;
	},
	validateEmpty(self, fieldValue, noticeMsg) {
		if(!fieldValue) {
			self.$message(noticeMsg + '不能为空！');
			return false;
		}
		return true;
	},
	validateNum(self, fieldValue, noticeMsg) {

		let value = parseInt(fieldValue);
		if(typeof value != "number") {
			self.self.$message(noticeMsg +'需为数字！');
			return false;
		}
		return true;
	},
	validateUpload(self, fieldValue, noticeMsg) {
		if(!fieldValue) {
			self.$message('请上传' + noticeMsg + "！");
			return false;
		}
		return true;
	},
	validateSelect(self, fieldValue, noticeMsg) {
		if(!fieldValue) {
			self.$message('请选择' + noticeMsg + "！");
			return false;
		}
		return true;
	},
	validateImgSizeAndType(self, file, imgSize, imgTypes) {
		if(imgTypes == undefined) {
			imgTypes = '.png|.jpg|.jpeg|.gif'
		}
		let imgType = file.name.substr(file.name.lastIndexOf('.') + 1);
		if(imgTypes.indexOf(imgType.toLowerCase()) == -1) {
			self.$message('只能上传' + imgTypes + '格式图片!');
			return false;
		}
		if(file.size / 1024 / 1024 > imgSize) {
			self.$message('上传头像图片大小不能超过 ' + imgSize + 'MB!');
			return false;
		}
		return true;
	},
	validateTel(self, tel) {
		var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
		if(!myreg.test(tel)) {
			self.$message('电话号码格式不正确！');
			return false;
		}
		return true;
	},
	
}